<?php

class editGroupRights extends bee\form
{ protected static $__formID = "frm200911252235";
  public $__caption = "Group rights editing";
  public $__version = "1.1";
  protected $__activeMenu = "groupRights";

  protected $gr_task;
  protected $arr_group;
  protected $arr_perden;
  protected $serror;

  protected $optGroup;
  protected $txtSysPage;
  protected $txtSysAction;
  protected $optPermission;

  public function createForm($evt)
  { $this->gr_task = new bee\gr_task();

    $group = new bee\group();
    $rsGroup = new bee\rowSet($group);
    $this->arr_group = array(-2 => "public", -1 => "all registered");
    bee\factory::save($this->arr_group     += $rsGroup->getList("name", "id"), "arrGroup",       "groupRights");
    $this->arr_perden = bee\getListByRange(3);


    if (isset($evt["id"]))
    { $this->gr_task->SELECT($evt["id"]);
    }

    $this->optGroup         = new bee\option("frm_group_id",   $this->gr_task->group_id, $this->arr_group, "name");
    $this->optGroup->notSelectedText = "select group";
    $this->optGroup->notSelectedValue = null;
    $this->txtSysPage       = new bee\text("frm_sys_page",     $this->gr_task->sys_page);
    $this->txtSysAction     = new bee\text("frm_sys_action",   $this->gr_task->sys_action);
    $this->optPermission    = new bee\option("frm_per_den",    $this->gr_task->per_den, $this->arr_perden);
    $this->optPermission->notSelectedText = "";
    $this->optPermission->notSelectedValue = null;

  }

  public function loadForm($evt)
  { $this->serror = new bee\serror();
  }

  public function drawForm($evt)
  {  $this->__DSDoc->setRow($this->gr_task);

  }

  public function edit($evt)
  { $this->gr_task->fillFromArray($evt, "frm_");
    if($this->validate($this->gr_task, $this->serror))
    { if($this->gr_task->id)
      { $this->gr_task->UPDATE();
        $this->__app->setMessage("Permission modified.");
      } else
      { $this->gr_task->INSERT();
        $this->__app->setMessage("Permission added.");
      }
    } else
    { $this->__app_>setMessage($this->serror->err_description, MSG_LEVEL_ERROR);
    }
  }

  public function delete($evt)
  { $this->gr_task->id = $evt["id"];
    $this->gr_task->DELETE();
    $this->__app_>setMessage("Permission deleted.");
    header("Location: index.php?__form=groupRights&sys_msg=" . urlencode($this->__app_>getMessageText()) . "&sys_msg_style=" . bee\app::getMessageLevel());
  }

  protected function validate(bee\gr_task $row, bee\serror &$error)
  { if(!$row->sys_page)
    { $error->err_code = -10;
      $error->err_description .= "\nPage is mandatory.";
      return false;
    }
    if(!$row->per_den)
    { $error->err_code = -11;
      $error->err_description .= "\nPermission not selected.";
      return false;
    }
    if(!$row->group_id)
    { $error->err_code = -12;
      $error->err_description .= "\nGroup not selected";
      return false;
    }
    $error->err_code = 0;
    return true;
  }
}